class: left, top, my-title, title-slide # Building a Sustainable R Ecosystem for Reporting ### Lindsey Dietz, PhD ### noRth 2021 Tutorial (September 29, 2021)
noRth 2021 Github
lindseydietz13@gmail.com
lindseydietz
lindseydietz13
--- # Disclaimer The views expressed in this presentation are strictly my own. They do not necessarily represent the position of the Federal Reserve Bank of Minneapolis or the Federal Reserve System. --- # Who am I? .pull-left[  ] .pull-right[ - Financial Economist working on stress testing models - Co-creator of the noRth conference - [R Ladies - Twin Cities meetup co-organizer](https://www.meetup.com/rladies-tc/members/) - [IF/THEN AAAS STEM Ambassador](https://www.ifthenshecan.org/ambassadors/) - [Hall of Fame college athlete](https://umdbulldogs.com/honors/umd-athletic-hall-of-fame/lindsey-dietz/130) ] --- # Goal of this tutorial Introduce R tools that help you communicate a data story in a professional, sustainable, and beautiful way.  --- # In this tutorial - How to set up your R ecosystem to support report creation in HTML (and PDF) -- - Examples of table and graphics packages to support analysis -- - Tips and tricks to make your reports dazzling and reproducible --- # Not in this tutorial >- Setting up version control - please use it! Check out Jenny Bryan's [Happy Git and Github with R](https://happygitwithr.com/) >- Details of the tables or graphics packages - these could easily be their own tutorials >- Shiny - we are going to focus on HTML (primary) and PDF (secondary) reports The good news is several speakers tomorrow will cover Shiny and other graphics applications! --- # Older process of reporting - Do analysis in one tool (R/SAS/SPSS/etc.) -- - Make tables and charts in another tool (Excel) -- - Paste everything into a text editor (Word/LaTeX) -- - Turn it into a final product (PDF) -- .center[  ] --- # Build an R ecosystem for Reporting  --- # Create workflows that shred  --- # R Project "R experts keep all the files associated with a project together — input data, R scripts, analytical results, figures. This is such a wise and common practice that RStudio has built-in support for this via projects." -- Hadley Wickham ([R for Data Science](https://r4ds.had.co.nz/workflow-projects.html)) -- R projects define your working directories: - Paths are relative rather than fixed - Your work is portable and tidy --- # Let's do it! 1. Open RStudio 2. Configure global options to avoid saving .Rdata - Tools -> Global Options -> Workspace -> Uncheck the box. Change the dropdown to Never. - Restart RStudio 3. Create an R project to associate with this talk or another set of files --- # Embrace the magic of R Markdown .center[] --- # What is R Markdown? R code + Markdown (text-to-HTML conversion tool) = R Markdown -- R Markdown files are designed to be used in three ways: -- - As an environment for you to do data science. -- - Collaborating with other data scientists with interest in your conclusions and how you reached them (the code). -- - **For communicating to decision makers, who want to focus on the conclusions, not the code behind the analysis.** --- # Let's do it! 1. Open the 0_dazzle.Rmd file inside the report folder - YAML Header - Code chunks - Markdown/LaTeX 2. Open the 1_dazzle.Rmd file inside the report folder - File organization 3. Open the 2_dazzle.Rmd file inside the report folder - Knitting with parameters 4. Open the 3_dazzle.Rmd file inside the report folder - Adding HTML and tables --- # Dazzling Elements in R Markdown - Using parameters in YAML header - Hiding code chunks and including 'Easter eggs' for other analysts - Using tabs and drop downs to improve pagination - Adding meaningful color to output - [Style output using HTML themes](https://www.datadreaming.org/post/r-markdown-theme-gallery/) --- # Folder Infrastructure .pull-left[ - Store long R scripts separate from your R Markdown files - Consider preprocessing and storage of summary data - Don't try to fit too much into one report; separate reports into their own folder ] .pull-right[  ] --- # Why HTML .pull-left[  ] -- .pull-right[ - HTML is *interactive* without being Shiny - HTML is *portable* and everyone you know has a web browser - TeX overhead can be high for creation of pdf and RMD files may fail to knit in different environments ] --- # If you must PDF - As long as you are not using LaTeX outside of R, you can use a minimal install of TeX via the `tinytex` package ```r install.packages('tinytex') tinytex::install_tinytex() ``` - [TinyTeX is a custom LaTeX distribution based on TeX Live that is small in size](https://yihui.org/tinytex/) - YMMV: errors can accumulate especially if you don't have admin rights on your computer --- # Step 3. Build your tabular content There are many table packages. For HTML, `DT` and `reactable` are good starting points. For pdf, `kableExtra` is the gold standard. .center[] --- # Step 4. Build your graphical content `ggplot2` works great as a static graphics package. For interactive graphics, use packages like `plotly` or `highcharter`. .center[] --- # Tips for Tables and Graphics - Create tables and graphics to support a narrative + Storyboard your ideas on paper or a whiteboard before diving in - Build the tables and graphics outside of R Markdown first to avoid extra loading overhead - Think about your user's needs + Do they like to download the data? + Are you using color in an intentional way? (1 in 12 men are colorblind) --- # Additional UX principles for reporting Your report should: - Have a clear hierarchy that makes navigation natural - Understand and embrace accessibility - Maintain time consistency - transition to or away from ideas slowly --- # Extensions - Version control of your reports - Deployment on a centralized server such as RStudio Connect opens up options such as scheduled reporting with email blasts - HTML reporting could be extended to true Shiny reporting if your users are asking for more interactivity --- # References and Credits - Artwork by Allison Horst sourced from [Github](https://github.com/allisonhorst/stats-illustrations) - [R for Data Science](https://r4ds.had.co.nz/workflow-projects.html) - [Advanced R](https://adv-r.hadley.nz/) - [Happy Git and Github with R](https://happygitwithr.com/) - [R Markdown Driven Development by Emily Riederer at rstudio::conf 2020](https://www.rstudio.com/resources/rstudioconf-2020/rmarkdown-driven-development/) <style type="text/css"> .remark-slide-content { font-size: 30px; padding: 1em 1em 1em 1em; } </style>